package com.xxq.gulimall.product.service.impl;

import com.xxq.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.xxq.gulimall.product.entity.AttrEntity;
import com.xxq.gulimall.product.service.AttrAttrgroupRelationService;
import com.xxq.gulimall.product.service.AttrService;
import com.xxq.gulimall.product.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxq.common.utils.PageUtils;
import com.xxq.common.utils.Query;

import com.xxq.gulimall.product.dao.AttrGroupDao;
import com.xxq.gulimall.product.entity.AttrGroupEntity;
import com.xxq.gulimall.product.service.AttrGroupService;

import javax.annotation.Resource;


@Slf4j
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService relationService;
    @Resource
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    // 预分页
    private IPage<AttrGroupEntity> getPage(Map<String, Object> params, QueryWrapper<AttrGroupEntity> queryWrapper) {
        String key = (String) params.get("key");
        if (key != null && !key.isEmpty()) {
            queryWrapper.like("attr_group_name", key).or()
                    .like("descript", key);
        }

        return this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );
    }

    @Override
    public PageUtils queryByCatelogId(Map<String, Object> params, Long catelogId) {
        //获取分类属性分组
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id", catelogId); // eq：等于
        IPage<AttrGroupEntity> page = getPage(params, queryWrapper);
        // 查询分类完整路径
        Long[] catelogPath = categoryService.getCatelogPath(catelogId);

        // 添加catelogPath字段
        page.getRecords().forEach(entity -> {
            entity.setCatelogPath(catelogPath);
        });

        return new PageUtils(page);
    }

    @Override // 获取属性分组的关联的所有属性 TODO 三表联合查询
    public List<AttrEntity> getRelation(Long attrGroupId) {
        return attrService.getRelation(attrGroupId);
    }

    @Override
    public void saveRelation(List<AttrAttrgroupRelationEntity> entityList) {
        relationService.saveBatch(entityList);
    }

    @Override // 获取分类下所有分组&关联属性 TODO 循环连接数据库
    public List<AttrGroupEntity> getGroupWithAttr(Long catelogId) {
        //获取分类属性分组
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id", catelogId); // eq：等于
        List<AttrGroupEntity> groupEntities = baseMapper.selectList(queryWrapper);

        groupEntities.forEach(entity -> {
            entity.setAttrs(attrService.getRelation(entity.getAttrGroupId()));
        });

        return groupEntities;
    }

    @Override // 删除属性与分组的关联关系
    public void deleteRelation(List<AttrAttrgroupRelationEntity> entityList) {
        ArrayList<Long> attrIds = new ArrayList<>();
        ArrayList<Long> groupIds = new ArrayList<>();
        entityList.forEach(entity -> {
            attrIds.add(entity.getAttrId());
            groupIds.add(entity.getAttrGroupId());
        });
        attrGroupDao.deleteRelation(attrIds, groupIds);
    }

    @Override
    public AttrGroupEntity getByIdWithCatelogPath(Long attrGroupId) {
        AttrGroupEntity attrGroup = this.getById(attrGroupId);
        // 获取分类路径
        Long[] catelogPath = categoryService.getCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return attrGroup;
    }

}